<!DOCTYPE html>
<html lang="en">
<head>
    <!-- 没有使用计算属性 -->
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>简单商品清单操作练习</title>
    <style type="text/css">
        *{
            padding: 5px;
            text-align: center;
        }
        #app table, td, th{
            border: 1px solid black;
            border-collapse: collapse;
        }
    </style>
</head>
<body>

    <div id="app">
        <table>
            <tr>
                <th>商品编号</th>
                <th>商品名称</th>
                <th>商品单价</th>
                <th>商品数量</th>
                <th>商品总价</th>
                <th>商品操作</th>
            </tr>
            <!-- 购物车为空时 -->
            <!-- 此处为什么item可以直接使用 -->
            <tr v-if="commodityInfo.every(item => item.nowNumber < 0 )">
                <td colspan="6">购物车为空</td>
            </tr>
            <template v-for="(item,index) in commodityInfo">
                <tr v-if="item.nowNumber>=0" :key="index">
                    <td>{{item.commdityId}}</td>
                    <td>{{item.commodityName}}</td>
                    <td>{{item.price}}</td>
                    <td>
                        <button @click="item.nowNumber--">&minus;</button>
                        <span>{{item.nowNumber}}</span>
                        <button @click="item.nowNumber++">+</button>
                    </td>
                    <td>{{item.price*item.nowNumber}}</td>
                    <td>
                        <!-- 删除数组数据 -->
                        <button @click="commodityInfo.splice(index,1)">删除</button>
                    </td>
                </tr>
            </template>
            
        </table>

        <br />
        <table>
            <tr>
                <th>总价</th>
                <th></th>
            </tr>
        </table>
    </div>

    <script src="vue.js"></script>
    <script>
        new Vue({
            el:"#app",
            data:function(){
                return {
                    // 商品 commodity
                    commodityInfo:[
                        { commdityId:1, commodityName:"苹果", price:10, nowNumber:0,},
                        { commdityId:2, commodityName:"香蕉", price:20, nowNumber:0,},
                        { commdityId:3, commodityName:"栗子", price:30, nowNumber:0,},
                        { commdityId:4, commodityName:"李子", price:40, nowNumber:0,}
                    ],
                    s:100,
                    a:200

                }
            },
            methods:{
                
            }
        })
    </script>
</body>
</html>